package com.test.auth_db_auto.auth_sms;

import com.test.auth_db_auto.handler.BootLoginFailureHandler;
import com.test.auth_db_auto.support.BootSecurityProperties;
import com.test.auth_db_auto.support.SmsUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @ Author: .Mr
 * @ ClassName SmsSecurityConfig
 * @ Description TODO
 * @ date 2022/1/5 11:21
 * @ Version 1.0
 * 手机号短信验证码 安全框架
 */
@Configuration
public class SmsSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    private SmsAuthenticationProvider authenticationProvider;
    private SmsCodeAuthenticationFilter authenticationFilter;
    private SmsCodeCheckFilter codeCheckFilter;

    public SmsSecurityConfig(SmsUserDetailService detailService,
                             StringRedisTemplate redisTemplate,
                             @Autowired(required = false)
                                     BootLoginFailureHandler failureHandler,
                             @Autowired(required = false)
                                     AuthenticationSuccessHandler successHandler,
                             BootSecurityProperties properties){
        this.authenticationFilter=new SmsCodeAuthenticationFilter(properties.getSmsLogin().getLoginProcessUrl());
        //设置成功失败处理器
        this.authenticationFilter.setAuthenticationFailureHandler(failureHandler);
        if(successHandler!=null){
            this.authenticationFilter.setAuthenticationSuccessHandler(successHandler);
        }

        //设置provider
        this.authenticationProvider=new SmsAuthenticationProvider();
        this.authenticationProvider.setUserDetailsService(detailService);

        this.codeCheckFilter=new SmsCodeCheckFilter(properties);
        this.codeCheckFilter.setFailureHandler(failureHandler);
//        this.codeCheckFilter.setTemplate(redisTemplate);
        this.codeCheckFilter.setSuccessHandler(successHandler);

    }

    @Override
    public void configure(HttpSecurity builder) throws Exception {

        this.authenticationFilter.setAuthenticationManager(builder.getSharedObject(AuthenticationManager.class));
        builder.authenticationProvider(this.authenticationProvider)
                .addFilterBefore(this.codeCheckFilter, UsernamePasswordAuthenticationFilter.class)
                .addFilterAfter(this.authenticationFilter,UsernamePasswordAuthenticationFilter.class);

    }
}
